package srst.ai;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import srst.util.FilePathUtil;
import srst.util.FileUtil;



public class CosinSimilarityUtil {
	
	//记录下resumeName 与 vector ，为计算kmeans提供数据
	private static HashMap<String, String> resume_vectormap = new HashMap<String, String>();
	
	private static double calculateCosineSimilarity(List<Double> dbl1, List<Double> dbl2)
	{
		double dblResult = 0.;
		double dbl1SquareSum = 0., dbl2SquareSum = 0.;

		for (int i = 0; i < dbl1.size(); i++) {			
			dbl1SquareSum += dbl1.get(i) * dbl1.get(i);
			dbl2SquareSum += dbl2.get(i) * dbl2.get(i);
			dblResult += Math.abs(dbl1.get(i)) * Math.abs(dbl2.get(i));
		}
		
		dbl1SquareSum = Math.sqrt(dbl1SquareSum);
		dbl2SquareSum = Math.sqrt(dbl2SquareSum);

		if (dbl1SquareSum != 0. && dbl2SquareSum != 0.) {
			dblResult = dblResult / (dbl1SquareSum * dbl2SquareSum);
		} else {
			dblResult = 0.0;
		}
		return dblResult;
	}
	
	
	

	public static List<String> computeCosSim2Text(String inputPath, String outputPath) throws IOException {
		BufferedReader br = FileUtil.getReader(inputPath);
		BufferedWriter bw = FileUtil.getWriter(outputPath);
		
		List<String> computeResult = new ArrayList<>();
		String lineTxt = "";
		while ((lineTxt = br.readLine()) != null) {
			String[] data = lineTxt.split(" ", 2);
			if (!resume_vectormap.containsKey(data[0])) {
				resume_vectormap.put(data[0], data[1]);
			}
		}
		
		for (String herbNameOne : resume_vectormap.keySet()) {
			String herbVec = resume_vectormap.get(herbNameOne);
			List<Double> herbOneVec =  handleHerbVec(herbVec);
			for (String herbNameTwo : resume_vectormap.keySet()) {
				if(!herbNameTwo.equals(herbNameOne)){
					herbVec = resume_vectormap.get(herbNameTwo);
					List<Double> herbTwoVec =  handleHerbVec(herbVec);
					double sim = calculateCosineSimilarity(herbOneVec, herbTwoVec);
					String result = herbNameOne + "\t" + herbNameTwo + "\t"+ sim ;
					bw.write(result);
					computeResult.add(result);
					bw.newLine();
					bw.flush();
				}
				
			}
		}

		if(br!=null){
			br.close();
		}
		if(bw!=null){
			bw.close();
		}
		return computeResult;

	}
	
	
	private static List<Double> handleHerbVec(String string){
		String[] values = string.split(" ");
		List<Double>   valueList= new ArrayList<>();  
		for(String temp : values){
			temp = temp.trim();
			if(!temp.equals("")){
				valueList.add(Double.valueOf(temp));
			}
		}
		return valueList;
	}



	public static HashMap<String, String> getHerbmap() {
		return resume_vectormap;
	}
	
	 

	public static void main(String[] args) throws FileNotFoundException {
		try {
			CosinSimilarityUtil.computeCosSim2Text(FilePathUtil.RESUME_VECTOR_PATH, FilePathUtil.COSINSIMILARITY_PATH);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("运行结束");
	}
}


